package com.cosina.game.tank.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.cosina.game.tank.ImageManager;
import com.cosina.game.tank.util.Direction;
import com.cosina.game.tank.util.DirectionAdapter;

public class ButtonAreaView extends View {

	
	private WarView warField;
	
	public ButtonAreaView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	int centerX = 64;
	int cneterY;
	@Override//96
	public void draw(Canvas canvas) {
		
		canvas.drawBitmap(ImageManager.enemyUp, 10,15, PaintSuite.TKV4Tank);
		try{
			canvas.drawText("" + warField.war.getPrepare(), 50, 40, PaintSuite.BRV4Text);
		}catch(Exception ex){
			
		}
//		canvas.drawBitmap(ImageManager.heart, 50,50, PaintSuite.TKV4Tank);
		cneterY = canvas.getHeight()/2;
		
		int radius = centerX -2; 
		int halfRadius = radius/2;
		RectF rectf = new RectF(centerX-radius,cneterY - radius, centerX + radius , cneterY + radius);
		RectF rectfInner = new RectF(centerX-halfRadius,cneterY - halfRadius, centerX + halfRadius , cneterY + halfRadius);
		
		if(Direction.right == clickedDirection){
			canvas.drawArc(rectf, -44, 88, true, PaintSuite.BRV4Clicked);
		}else{
			canvas.drawArc(rectf, -44, 88, true, PaintSuite.BRV4Button);
		}
		canvas.drawArc(rectfInner, -44, 88, true, PaintSuite.BRV4Inner);
		
		if(Direction.down == clickedDirection){
			canvas.drawArc(rectf, 46, 88, true, PaintSuite.BRV4Clicked);
		}else{
			canvas.drawArc(rectf, 46, 88, true, PaintSuite.BRV4Button);
		}
		canvas.drawArc(rectfInner, 46, 88, true, PaintSuite.BRV4Inner);
		
		if(Direction.left == clickedDirection){
			canvas.drawArc(rectf, 136, 88, true, PaintSuite.BRV4Clicked);
		}else{
			canvas.drawArc(rectf, 136, 88, true, PaintSuite.BRV4Button);
		}
		canvas.drawArc(rectfInner, 136, 88, true, PaintSuite.BRV4Inner);
		
		if(Direction.up == clickedDirection){
			canvas.drawArc(rectf, 226, 88, true, PaintSuite.BRV4Clicked);
		}else{
			canvas.drawArc(rectf, 226, 88, true, PaintSuite.BRV4Button);
		}
		canvas.drawArc(rectfInner, 226, 88, true, PaintSuite.BRV4Inner);
		
		postInvalidateDelayed(1000);
	}
	private Direction  clickedDirection = null;
	
	private long lastEventTime;
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		if(event.getPressure() < 0.15f){
			return true;
		}
		
		
		if(event.getEventTime() - lastEventTime <= 40){
			return true;
		}
		
		lastEventTime = event.getEventTime();
		
		float xSpan = event.getX() - centerX;
		float ySpan = event.getY() - cneterY;
		
		float factor = xSpan/ySpan;
		Direction direction = null;
		if(factor <= 0){
			//��һ����
			if(xSpan>=0){
				if(factor<=-1){
					direction = Direction.right;
				}else{
					direction = Direction.up;
				}
			}else{
				//��������
				if(factor<=-1)
					direction = Direction.left;
				else
					direction = Direction.down;
			}
		}else {
			if(xSpan>=0){
				if(factor>1){
					direction = Direction.right;
				}else{
					direction = Direction.down;
				}
			}else{
				if(factor>1){
					direction = Direction.left;
				}else{
					direction = Direction.up;
				}
			}
		}
		this.clickedDirection = direction;
		addSelectEffect(clickedDirection);
		DirectionAdapter adapter = getDirectionAdapter();
		if(null != adapter){
			adapter.doSwtich(direction);
		}
		
		return true;
	}
	
	private void addSelectEffect(Direction newSelected){
		this.invalidate();
		this.removeCallbacks(removeClicked);
		this.postDelayed(removeClicked, 500);
	}
	
	private Runnable removeClicked = new Runnable(){
		public void run(){
			ButtonAreaView.this.clickedDirection = null;
			ButtonAreaView.this.invalidate();
		}
	};


	
	public DirectionAdapter getDirectionAdapter() {
		try{
			return warField.war.justiceTank.moveDirectionAdapter;
		}catch (NullPointerException e) {
		}
		return null;
	}

	public void setDirectionAdapterOwner(WarView warField) {
		this.warField = warField;
	}
	
	
	

	
}
